package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.convert.YwComQyxxTwoConvertor;
import com.augurit.gzcgwsp.ywcmn.dao.YwComQyxxTwoDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxxTwo;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxTwoService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxTwoForm;

@Service
@Transactional
public class YwComQyxxServiceTwoImpl implements IYwComQyxxTwoService{

	@Autowired
	private YwComQyxxTwoDao ywComQyxxTwoDao;
	
	@Transactional(readOnly = true)
	public YwComQyxxTwoForm get(Long id) {
		YwComQyxxTwo entity = ywComQyxxTwoDao.get(id);
		return YwComQyxxTwoConvertor.convertVoToForm(entity);
	}
	
	@Transactional(readOnly = true)
    public YwComQyxxTwoForm getFormBySqdwTwo(String sqdwTwo,String templateCodeTwo) {
        YwComQyxxTwo entity = ywComQyxxTwoDao.findUnique("from YwComQyxxTwo t where t.sqdwTwo = '" + sqdwTwo+ "' and t.templateCodeTwo='"+templateCodeTwo+"'");
        if (entity == null) return null;
        return YwComQyxxTwoConvertor.convertVoToForm(entity);
    }
	
	public void delete(Long... ids) {
		ywComQyxxTwoDao.delete(ids);
	}

	public void save(YwComQyxxTwoForm... forms) {
		if(forms != null)
			for(YwComQyxxTwoForm form : forms)
				this.save(form);
	}
	
	public void update(YwComQyxxTwoForm form) {
	    YwComQyxxTwoForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwComQyxxTwoForm form){
		
		if(form != null){
			YwComQyxxTwo entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywComQyxxTwoDao.getYwComQyxxTwo(form.getId());
				if(entity.getId()==null){
					entity = new YwComQyxxTwo();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwComQyxxTwo();
			}
			
			YwComQyxxTwoConvertor.convertFormToVo(form, entity);
			
			ywComQyxxTwoDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwComQyxxTwoForm> search(Page<YwComQyxxTwoForm> page, YwComQyxxTwoForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwComQyxxTwo ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if (StringUtils.isNotBlank(form.getSqdwTwo())) {
		        hql.append(" and ps.sqdwTwo like '%" + form.getSqdwTwo() + "%'");
		    }
		    if(StringUtils.isNotBlank(form.getTemplateCodeTwo())){
		    	if(form.getTemplateCodeTwo().contains("jzljczz")){//余泥处--处置证
		    		hql.append(" and ps.templateCodeTwo like '%jzljczz%'");
		    	}else if(form.getTemplateCodeTwo().contains("jzfqwczzpf")){//余泥处--排放
		    		hql.append(" and ps.templateCodeTwo like '%jzfqwczzpf%'");
		    	}else if(form.getTemplateCodeTwo().contains("jzfqwczzxn")){//余泥处--排放
		    		hql.append(" and ps.templateCodeTwo like '%jzfqwczzxn%'");
		    	}else if(form.getTemplateCodeTwo().contains("gdqy")){//燃气--管道
		    		hql.append(" and ps.templateCodeTwo like '%gdqy%'");
		    	}else if(form.getTemplateCodeTwo().contains("pzqy")){//燃气--瓶装液化气
		    		hql.append(" and ps.templateCodeTwo like '%pzqy%'");
		    	}else if(form.getTemplateCodeTwo().contains("csdllszysp")){//市容--占用
		    		hql.append(" and ps.templateCodeTwo like '%csdllszysp%'");
		    	}else if(form.getTemplateCodeTwo().contains("csdlwj")){//市容--挖掘
		    		hql.append(" and ps.templateCodeTwo like '%csdlwj%'");
		    	}else{
		    		hql.append(" and ps.templateCodeTwo = '" + form.getTemplateCodeTwo() + "'");
		    	}
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		ywComQyxxTwoDao.findPage(pg, hql.toString(), values);
		
		List<YwComQyxxTwoForm> list = YwComQyxxTwoConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwComQyxxTwoForm> search(Page<YwComQyxxTwoForm> page, List<PropertyFilter> filters) {
		Page<YwComQyxxTwo> pg = PageUtils.newPageInstance(page);
		
		ywComQyxxTwoDao.findPage(pg, filters);
		
		List<YwComQyxxTwoForm> list = YwComQyxxTwoConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwComQyxx(YwComQyxxTwoForm form) {
	    YwComQyxxTwoForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwComQyxxTwo entity = null;
			entity = ywComQyxxTwoDao.getYwComQyxxTwo(dbform.getId());
			YwComQyxxTwoConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				ywComQyxxTwoDao.updateYwComQyxxTwo(entity);
			}
	    }
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService#getYwComQyxx(java.lang.Long, java.lang.String)
	 */
    public YwComQyxxTwo getYwComQyxx(Long qyid, String acceptNo) {
        return ywComQyxxTwoDao.getYwComQyxxTwo(qyid);
    }

    public void saveEntity(YwComQyxxTwo entity) {
       this.ywComQyxxTwoDao.save(entity);
        
    }

	public Map<Long, String> getQyxx() {
	 String sql="select t.id,t.sqdwTwo from "+YwComQyxxTwo.class.getName()+" t";
	 List<Object[]> list=this.ywComQyxxTwoDao.find(sql);
	 Map<Long,String> map=new HashMap<Long,String>();
	 for(Object[] objs:list){
		 Long id=(Long) objs[0];
		 String sqdwTwo=(String) objs[1];
		 map.put(id, sqdwTwo);
	 }
	 return map;
	 
	}

	/**
	 * 保存企业信息返回id
	 */
	public Long saveEnterpriseInfo(YwComQyxxTwoForm form) {
		YwComQyxxTwo entity =new YwComQyxxTwo();
		
		YwComQyxxTwoConvertor.convertFormToVo(form, entity);
		
		return ywComQyxxTwoDao.saveEntity(entity);
	}

	/*
	 * 
	 * @see com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService#getEntity(java.lang.String, java.lang.String)
	 */
	public YwComQyxxTwo getEntity(String sqdwTwo, String tempcodeTwo) {
		String sql=" from "+YwComQyxxTwo.class.getName()+" where sqdwTwo='"+sqdwTwo+"'"+" and templateCodeTwo='"+tempcodeTwo+"'";
		List<YwComQyxxTwo> list=this.ywComQyxxTwoDao.find(sql);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public YwComQyxxTwoForm getFormBySqdwTwo(String sqdwTwo) {
		YwComQyxxTwo entity = ywComQyxxTwoDao.findUnique("from YwComQyxxTwo t where t.sqdwTwo = '" + sqdwTwo+ "'");
        if (entity == null) return null;
        return YwComQyxxTwoConvertor.convertVoToForm(entity);
	}
}
